package com.zhs.study.source;


import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

/**
 *
 * @Author zhenghuisheng
 * @Date：2025/11/21 16:28
 */
public class ProducerDemo {

    public static void main(String[] args) throws MQBrokerException, RemotingException, InterruptedException, MQClientException {

        // 1. 创建生产者（Producer Group 名称）
        DefaultMQProducer producer = new DefaultMQProducer("zhsProducerGroup");
        producer.setNamesrvAddr("192.168.1.246:9876");

        // 2. 启动 Producer（不启动会报错）
        producer.start();
        System.out.println("Producer 启动完成");

        // 3. 构建消息对象
        Message msg = new Message("zhsTopic", "TagA", "Demo Message".getBytes());

        // 4. 发送消息
        System.out.println("开始发送消息...");
        producer.send(msg);
        System.out.println("消息发送完成");

        // 5. 关闭 Producer
        producer.shutdown();

    }
}